// ucenter-behavior.js
const app = getApp();
module.exports = Behavior({
  behaviors: [],
  properties: {
  },
  data: {
  },
  attached: function () {
    app.eventBus.on(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate, this);
    app.eventBus.on(app.Events.UserProfileUpdate, this.eventOnUserProfileUpdate, this);
    app.eventBus.on(app.Events.UcenterInfoCompleteChange, this.eventOnUcenterInfoCompleteChange, this);
    app.eventBus.on(app.Events.ClearMessageCount, this.eventOnClearMessageCount, this);
    app.eventBus.on(app.Events.RefreshUserCenter, this.eventRefreshUserCenter, this);
  },
  detached: function() {
    app.eventBus.off(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
    app.eventBus.off(app.Events.UserProfileUpdate, this.eventOnUserProfileUpdate);
    app.eventBus.off(app.Events.UcenterInfoCompleteChange, this.eventOnUcenterInfoCompleteChange);
    app.eventBus.off(app.Events.ClearMessageCount, this.eventOnClearMessageCount);
    app.eventBus.off(app.Events.RefreshUserCenter, this.eventRefreshUserCenter);
  },
  methods: {
    eventOnUserinfoUpdate: function (userinfo) {
      this.setData({'summary.avatar':userinfo.avatar, 'summary.nickname':userinfo.nickname})
    },
    eventOnUserProfileUpdate: function(profile) {
      this.setData({'summary.avatar':profile.avatar, 'summary.nickname':profile.nickname});
    },
    eventOnUcenterInfoCompleteChange: function(complete) {
      this.setData({'summary.info_complete':complete});
      this._drawCanvas(this.data.canvas, 60 / 2, '#FF770A', '完成', complete, 100);
    },
    eventOnClearMessageCount: function(count) {
      let total = this.data.summary.message_count - count;
      this.setData({'summary.message_count': total<0?0:total});
    },
    eventRefreshUserCenter: function () {
      this.setData({lastRefreshTime: 0});
    },

    bindTapTimeline() {
      this.setData({['summary.discover_post_life_count']:0})
    },

    _createCanvas: function (id, xy, color, txt, val, total) {
      let ctx = this.data.canvas;
      if(!ctx) {
        ctx = wx.createCanvasContext(id, this);
        this.setData({canvas: ctx});
      }
      this._drawCanvas(ctx, xy, color, txt, val, total);
    },
    _drawCanvas: function(ctx, xy, color, txt, val, total) {
      ctx.setLineWidth(4);
      ctx.setStrokeStyle('#E2E2E2');
      ctx.setLineCap('round');
      ctx.beginPath();
      ctx.arc(xy, xy, 0.75 * xy, 0, 2 * Math.PI, false);
      ctx.stroke();

      ctx.setLineWidth(4);
      ctx.setStrokeStyle(color);
      ctx.setLineCap('round');
      let p = val / total;

      ctx.beginPath(xy);
      ctx.arc(xy, xy, 0.75 * xy, -90 * Math.PI / 180, (p * 360 - 90) * Math.PI / 180, false);
      ctx.textAlign = "center";
      ctx.font = '12px Arial';
      ctx.fillStyle ='#ff7100'
      ctx.fillText(txt, xy, 1.4 * xy, xy);
      // ctx.font = '28rpx Arial';
      // ctx.fillStyle = color;
      ctx.fillText(val+"%", xy, 1.0 * xy, xy);
      ctx.stroke();//对当前路径进行描边
      ctx.draw();
    }
  }
});
